package com.mobile.service.impl;

import com.github.pagehelper.PageInfo;
import com.mobile.service.convert.UnemploymentDeclarationConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.UnemploymentDeclarationDTO;
import com.mobile.service.dto.UnemploymentDeclarationQueryDTO;
import com.mobile.service.entity.UnemploymentDeclaration;
import com.mobile.service.inter.UnemploymentDeclarationService;
import com.mobile.service.mapper.UnemploymentDeclarationMapper;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 失业保险申请表service实现类
 */
@Service("unemploymentDeclarationService")
@Transactional
public class UnemploymentDeclarationServiceImpl implements UnemploymentDeclarationService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private UnemploymentDeclarationConverter unemploymentDeclarationConverter;
    @Autowired
    private UnemploymentDeclarationMapper unemploymentDeclarationMapper;

    @Override
    public Map<String, Object> addUnemploymentDeclaration(UnemploymentDeclarationDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        UnemploymentDeclarationDTO pd = unemploymentDeclarationMapper.getUnemploymentDeclarationByApplicantName(dto.getApplicantName());
        if (null == pd) {
            unemploymentDeclarationMapper.insertUnemploymentDeclaration(unemploymentDeclarationConverter.toEntity(dto));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        } else {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该申请人姓名"));
        }
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageResult<UnemploymentDeclarationDTO> getUnemploymentDeclarationListPage(UnemploymentDeclarationQueryDTO queryDTO) throws Exception {
        PageUtils.startPage(queryDTO);
        List<UnemploymentDeclarationDTO> list = unemploymentDeclarationMapper.unemploymentDeclarationlistPage(queryDTO);
        return new PageResult<UnemploymentDeclarationDTO>(list);
    }

    @Override
    public UnemploymentDeclarationDTO getUnemploymentDeclarationById(String id) throws Exception {
        return unemploymentDeclarationMapper.getUnemploymentDeclarationById(id);
    }

    @Override
    public Map<String, Object> updateUnemploymentDeclaration(UnemploymentDeclarationDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String pkValue = dto.getId();
        if (pkValue == null) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        UnemploymentDeclarationDTO data = this.getUnemploymentDeclarationById(pkValue);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        UnemploymentDeclarationDTO pd = unemploymentDeclarationMapper.getUnemploymentDeclarationByApplicantName(dto.getApplicantName());
        if (null == pd || pd.getId().equals(pkValue)) {
            unemploymentDeclarationMapper.updateUnemploymentDeclaration(unemploymentDeclarationConverter.toEntity(dto));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        } else {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该申请人姓名"));
        }
        return map;
    }

    @Override
    public Map<String, Object> removeUnemploymentDeclaration(String id) throws Exception {
        Map<String, Object> map = new HashMap<>();
        unemploymentDeclarationMapper.deleteUnemploymentDeclaration(id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<UnemploymentDeclarationDTO> getUnemploymentDeclarationList(UnemploymentDeclarationQueryDTO queryDTO) throws Exception {
        return unemploymentDeclarationMapper.getUnemploymentDeclarationList(queryDTO);
    }
}
